/*
 * User: rant
 * Date: Aug 1, 2007
 *
 *
 */
package laMalinche.actObservable;

import laMalinche.rete.nodes.StopEvaluatingException;
import laMalinche.rete.rule.Action;
import laMalinche.rete.rule.ActionType;
import laMalinche.DevMode;

import java.util.*;

/**
 *
 */
public class BasicObservableAction extends BasicActObservable implements Action {

	private Action action;

	public BasicObservableAction(final Action action) {
         DevMode.getInstance().getInjector().injectMembers(this);
        this.action = action;
	}

	public Action getAction() {
		return action;
	}

	public void setAction(final Action action) {
		this.action = action;
	}

	public ActionType getActionType() {
		return getAction().getActionType();
	}

	public void execute() {
        //		ProcessModel policy = (ProcessModel) evaluation.getEntity();
		adviseStarted();
        boolean executed = true;
        try {
			getAction().execute();
		} catch (StopEvaluatingException e) {
			executed = false;
		}
         catch (Exception e) {
            adviseFinished(false);
           throw new RuntimeException(e);
        }
        adviseFinished(executed);
	}

	public String getName() {
		return getAction().getName();
	}

	@Override
	protected Set<String> getExcludedFields() {
		return null;
	}

	@Override
	public boolean equals(Object actionAdapter) {
        return actionAdapter instanceof BasicObservableAction &&
                getAction().equals(((BasicObservableAction) actionAdapter).getAction()) ||
                actionAdapter instanceof Action && getAction().equals(actionAdapter);
	}

	@Override
	public int hashCode() {
		return getAction().hashCode();
	}
}